package com.etc.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.etc.entity.UserInfo;
import com.fasterxml.jackson.databind.ObjectMapper;

/**获取服务器中的session
 * 念旭麟
 * Servlet implementation class SessionServlet
 */
@WebServlet("/SessionServlet")
public class SessionServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String op = request.getParameter("op");
		if("usersession".equals(op)){
			dousersession(request,response);
		}
	}

	private void dousersession(HttpServletRequest request, HttpServletResponse response) throws IOException {
		//获取session  
		HttpSession session = request.getSession();
		//获取用户对象
		UserInfo userInfo = (UserInfo)session.getAttribute("USER_LOGIN");
		// 响应
		ObjectMapper objectMapper = new ObjectMapper();
		String json = objectMapper.writeValueAsString(userInfo);
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.write(json);
	}

}
